<?php
///关于标收费信息的统计
if(!isset($dbselect)) //前面没设置 数据库 则指定使用 DB3
    $dbselect   = Db::G3();
$sfwhere      = "";
if($startdate!="" && $enddate!=""){
  $sfwhere    = " and qtime_h>='$startdate' and  qtime_h<='$enddate'";  
}
$gfileds        = "SUBSTR(qtime_h,1,10 )";
if($MOD=="M"){
    $gfileds    = "SUBSTR(qtime_h,1,7)";    
}    

//每日活期总量买入统计
$rows = $dbselect->GetRows("select sum(money) as money,SUBSTR( time_h, 1, 10 ) as time from 23sm_v2_5order_huoqi 
                            where time_h>='".$startdate."' and time_h<='".$enddate."' group by SUBSTR( time_h, 1, 10 )");
foreach($rows as $row){
    $money = $row['money'];
    $ctime = $row['time'];
    $money = isset($money) ? $money : 0;        
    CTLog::G()->pxxDayReal("sid",1,"活期买入",$money,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"活期买入",$money,$ctime);
}
                                                
//每日活期赎回总量统计  每日活期赎回本金统计  日活期赎回利息统计  活期赎回人数
$rows = $dbselect->GetRows("select sum(redeem_tot_money) as money,sum(redeem_benjin) as redeem_benjin,sum(redeem_lixi) as redeem_lixi,SUBSTR( time_h, 1, 10 ) as time,COUNT(DISTINCT(uid)) as number
                           from 23sm_v2_5redeem_huoqi 
                           where `status`=2 and time_h>='".$startdate."' and time_h<='".$enddate."' group by SUBSTR( time_h, 1, 10 )");
foreach($rows as $row){
   
    $ctime          = $row['time'];
    $money          = getReq($row,'money',0);
    $redeem_benjin  = getReq($row,'redeem_benjin',0);
    $redeem_lixi    = getReq($row,'redeem_lixi',0);
    $number         = getReq($row,'number',0); 
    CTLog::G()->pxxDayReal("sid",1,"活期赎回",$money,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"活期赎回",$money,$ctime);
    CTLog::G()->pxxDayReal("sid",1,"活期日赎回本金",$redeem_benjin,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"活期日赎回本金",$redeem_benjin,$ctime);
    CTLog::G()->pxxDayReal("sid",1,"活期日赎回利息",$redeem_lixi,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"活期日赎回利息",$redeem_lixi,$ctime);
    CTLog::G()->pxxDayReal("sid",1,"活期赎回人数",$number,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"活期赎回人数",$number,$ctime);
}

//每日定期总量买入统计

$rows = $dbselect->GetRows("select sum(money) as money,SUBSTR( time_h, 1, 10 ) as time from 23sm_v2_5order_dingqi 
                            where time_h>='".$startdate."' and time_h<='".$enddate."' group by SUBSTR( time_h, 1, 10 )");
foreach($rows as $row){
    $money = $row['money'];
    $ctime = $row['time'];
    $money = isset($money) ? $money : 0;    	
    CTLog::G()->pxxDayReal("sid",1,"定期买入",$money,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"定期买入",$money,$ctime);
}
	


//每日定期到期总还款统计

$rows = $dbselect->GetRows("SELECT date as time,SUM(redeem_tot_money) money,SUM(redeem_benjin) redeem_benjin, SUM(redeem_lixi) redeem_lixi,COUNT(DISTINCT(uid)) as number
                            FROM 23sm_v2_5redeem_dingqi 
                            WHERE `status`=2 and date>='".$startdate."' and date<='".$enddate."' group by date");
foreach($rows as $row){
    
    $ctime          = $row['time'];
    $money          = getReq($row,'money',0);
    $redeem_benjin  = getReq($row,'redeem_benjin',0);
    $redeem_lixi    = getReq($row,'redeem_lixi',0);
    $number         = getReq($row,'number',0);
    CTLog::G()->pxxDayReal("sid",1,"定存还款",$money,$ctime);  
    CTLog::G()->pxxDayReal("cid",1,"定存还款",$money,$ctime);
    CTLog::G()->pxxDayReal("sid",1,"定期日赎回本金",$redeem_benjin,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"定期日赎回本金",$redeem_benjin,$ctime);
    CTLog::G()->pxxDayReal("sid",1,"定期日赎回利息",$redeem_lixi,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"定期日赎回利息",$redeem_lixi,$ctime);
    CTLog::G()->pxxDayReal("sid",1,"定存到期回款人数",$number,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"定存到期回款人数",$number,$ctime);
    
} 

//每日签到收费
$rows = $dbselect->GetRows("SELECT FORMAT(SUM(FORMAT(money,2)),2) as lixi,signin_date as time
							FROM 23sm_5signin WHERE  signin_date>='".$startdate."' and signin_date<='".$enddate."' group by signin_date");

foreach($rows as &$row){
	$money = isset($row['lixi']) ? $row['lixi'] : 0;    
	$ctime = $row['time'];	
    CTLog::G()->pxxDayReal("sid",1,"网站_支出_每日签到",$money,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"网站_支出_每日签到",$money,$ctime);
}

$rows = $dbselect->GetRows("SELECT SUM(interest) as money,date as time FROM  `23sm_v2_5lixi_log`  
                            WHERE licai_type=1 and date>='".$startdate."' and date<='".$enddate."' group by date");
foreach($rows as $row){
    $money = $row['money'];
    $ctime = $row['time'];
    $money = isset($money) ? $money : 0;
    CTLog::G()->pxxDayReal("sid",1,"日活期产生利息",$money,$ctime);
    CTLog::G()->pxxDayReal("cid",1,"日活期产生利息",$money,$ctime);
}
	

//日定存还款利息
$rows = $dbselect->GetRows("SELECT SUM(interest) money,date as time FROM  `23sm_v2_5lixi_log` 
                            WHERE licai_type=2 and date>='".$startdate."' and date<='".$enddate."' group by date");

foreach($rows as $row){
    $money = $row['money'];
    $ctime = $row['time'];
    $money = isset($money) ? $money : 0;
    CTLog::G()->pxxDayReal("sid",1,"日定存还款利息",$money,$ctime);	
    CTLog::G()->pxxDayReal("cid",1,"日定存还款利息",$money,$ctime);
}


//活期---每日待赎回总金额---add by clw 2016-03-18
#某天活期本金和利息
    //SELECT date time,SUM(wait_money+interest) AS money ,SUM(wait_money) AS benjin,SUM(interest) AS lixi FROM `23sm_v2_5lixi_log` 
    //WHERE `date`= '2015-12-31' AND licai_type=1 AND `status`=1;
//$rows = $dbselect->GetRows("SELECT sum(wait_tot_benjin+wait_tot_lixi) AS money,sum(wait_tot_benjin) as benjin,sum(wait_tot_lixi) as lixi,SUBSTR( last_do_time, 1, 10 ) as last_do_time 
//                            FROM 23sm_v2_5trade_huoqi WHERE  last_do_time>='".$startdate."' and last_do_time<='".$enddate."' group by SUBSTR( last_do_time, 1, 10 )"); 
$rows   = $dbselect->GetRows("SELECT date time,SUM(wait_money+interest) AS money ,SUM(wait_money) AS benjin,SUM(interest) AS lixi FROM `23sm_v2_5lixi_log` 
                            WHERE  date>='".$startdate."' and date<='".$enddate."' AND licai_type=1 AND `status`=1 group by date");
foreach($rows as $row){
    $ctime  = $row['time']; 
    $money  = getReq($row,'money',0);
    $benjin = getReq($row,'benjin',0); 
    $lixi   = getReq($row,'lixi',0);   
    if($money>0){
        CTLog::G()->pxxDayReal("sid",1,"活期待赎回",$money,$ctime);
        CTLog::G()->pxxDayReal("cid",1,"活期待赎回",$money,$ctime);
    }
    if($benjin>0){
        CTLog::G()->pxxDayReal("sid",1,"活期待赎回_本金",$benjin,$ctime);
        CTLog::G()->pxxDayReal("cid",1,"活期待赎回_本金",$benjin,$ctime);
    }
    if($lixi>0){
        CTLog::G()->pxxDayReal("sid",1,"活期待赎回_利息",$lixi,$ctime);
        CTLog::G()->pxxDayReal("cid",1,"活期待赎回_利息",$lixi,$ctime);
    }
}

//定期---每日待赎回总金额---add by clw 2016-03-18
$rows = $dbselect->GetRows("SELECT SUBSTR( time_h, 1, 10 ) time,SUM(wait_money+interest) AS money ,SUM(wait_money) AS benjin,SUM(interest) AS lixi FROM `23sm_v2_5lixi_log` 
                             WHERE  time_h>='".$startdate."' and time_h<='".$enddate."' AND licai_type=2 AND `status`=1 group by SUBSTR( time_h, 1, 10 )");
//var_dump("SELECT SUBSTR( time_h, 1, 10 ) time,SUM(wait_money+interest) AS money ,SUM(wait_money) AS benjin,SUM(interest) AS lixi FROM `23sm_v2_5lixi_log` 
                             //WHERE  time_h>='".$startdate."' and time_h<='".$enddate."' AND licai_type=2 AND `status`=1 group by SUBSTR( time_h, 1, 10 )");
foreach($rows as $row){
    $ctime  = $row['time']; 
    $money  = getReq($row,'money',0);
    $benjin = getReq($row,'benjin',0); 
    $lixi   = getReq($row,'lixi',0);  
    if($money){
        CTLog::G()->pxxDayReal("sid",1,"定期待赎回",$money,$ctime);
        CTLog::G()->pxxDayReal("cid",1,"定期待赎回",$money,$ctime);	
    }
    if($benjin){
        CTLog::G()->pxxDayReal("sid",1,"定期待赎回_本金",$benjin,$ctime);	
        CTLog::G()->pxxDayReal("cid",1,"定期待赎回_本金",$benjin,$ctime);
    }
    if($lixi){
        CTLog::G()->pxxDayReal("sid",1,"定期待赎回_利息",$lixi,$ctime);
        CTLog::G()->pxxDayReal("cid",1,"定期待赎回_利息",$lixi,$ctime);	
    }
}

//每日体验金券利息支出
$rows = $dbselect->GetRows("SELECT SUM(money) as money  FROM 21h_0hongbao WHERE  
		time_use>='".$startdate."' and time_use<='".$enddate."' and money_ty=8 ");
$apr = cfg::get("licai_tiyanapr",0);

foreach($rows as &$row){
	$money = isset($row['money']) ? $row['money'] : 0;
	$lixi = $money * $apr / 36500;
	$ctime = date("Y-m-d ",strtotime("-1 day"));
	CTLog::G()->pxxDayReal("sid",1,"体验标利息_实_总",$lixi,$ctime);
	CTLog::G()->pxxDayReal("cid",1,"体验标利息_实_总",$lixi,$ctime);
}























